<?php
require_once BASEPATH."application/default/models/Do/News.php";

class FormNews
{
	protected $_doNews;

	protected $_show = 5;

	public $submit;

	/** public function __construct() {{{
	 *
	 *
	 */
	public function __construct()
	{
		$this->_doNews = new Do_News();
	}
	/*}}}*/

	/** public function count() {{{
	 *
	 *
	 */
	public function count()
	{
		$db = $this->_doNews->getAdapter()->getConnection();
		$stm = $db->query('SELECT COUNT(*) AS Total FROM tb_news');
		$obj = $stm->fetchObject();

		return $obj->Total;
	}
	/*}}}*/

	/** public function lista() {{{
	 *
	 *
	 */
	public function lista()
	{
		$stm = $this->_doNews->fetchAll(null,'date');

		return $stm;
	}
	/*}}}*/

	/** public function find($id) {{{
	 *
	 *
	 */
	public function find($id)
	{
		$rowSet = $this->_doNews->find($id);

		return $rowSet->current();
	}
	/*}}}*/

	/** public function getDo() {{{
	 *
	 *
	 */
	public function getDo()
	{
		return $this->_doNews;
	}
	/*}}}*/

	/** public function getListJson() {{{
	 *
	 *
	 */
	public function getListJson()
	{
		$array = $this->lista()->toArray();
	
		$id = 0;
		foreach($array as $val)
		{
			$response->rows[$id]['id'] = (int)$val['cd_news'];
			$response->rows[$id]['cell'][0] = (int)$val['cd_news'];
			$response->rows[$id]['cell'][] = $val['title'];
			$id++;
		}

		return Zend_Json::encode($response);
	}
	/*}}}*/

	/** public function insert($request) {{{
	 *
	 *
	 */
	public function insert($request)
	{
		$array = array();
		$cols = $this->_doNews->info();

		foreach($cols['cols'] as $valCols)
		{
			$array[$valCols] = $request[$valCols];
		}

		if(strlen($array['title']) <= 0)
		{
			return "Favor preencher o Título.";
		}

		$this->_doNews->insert($array);

		return "Notícia salva.";
	}
	/*}}}*/

	/** public function update($request) {{{
	 *
	 *
	 */
	public function update($request)
	{
		$array = array();
		$cols = $this->_doNews->info();

		foreach($cols['cols'] as $valCols)
		{
			$array[$valCols] = $request[$valCols];
		}
		if(strlen($array['title']) <= 0)
		{
			return "Favor preencher o Título.";
		}
		unset($array["cd_news"]);

		$where = $this->_doNews->getAdapter()->quoteInto('cd_news = ?', $request["cd_news"]);
		$this->_doNews->update($array, $where);

		return "Notícia atualizada.";
	}
	/*}}}*/

	/** public function delete($id) {{{
	 *
	 *
	 */
	public function delete($id)
	{
		$where = $this->_doNews->getAdapter()->quoteInto('cd_news = ?', $id);
		$this->_doNews->delete($where);

		return "Notícia excluida.";
	}
	/*}}}*/
}
?>
